<template>
    <div>
        <div style="display: flex;justify-content: space-between">
            <el-button icon="el-icon-plus" type="primary" @click="showAddSalaryView">添加助理套餐</el-button>
            <el-button icon="el-icon-refresh" type="success" @click="initSalaries"></el-button>
        </div>
        <div style="margin-top: 10px">
            <el-table :data="salaries" border stripe>
                <el-table-column align="center" type="selection" width="160"></el-table-column>
                <el-table-column align="center" width="200" prop="duty" label="担任职责"></el-table-column>
                <el-table-column align="center" width="200" prop="launch" label="午餐补助"></el-table-column>
                <el-table-column align="center" width="200" prop="reward" label="奖金"></el-table-column>
                <el-table-column align="center" label="操作">
                    <template slot-scope="scope">
                        <el-button @click="showEditSalaryView(scope.row)">编辑</el-button>
                        <el-button type="danger" @click="deleteSalary(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-dialog
                :title="dialogTitle"
                :visible.sync="dialogVisible"
                width="50%">
            <div style="display: flex;justify-content: space-around;align-items: center">
                <el-steps direction="vertical" :active="activeItemIndex">
                    <el-step :title="itemName" v-for="(itemName,index) in salaryItemName" :key="index"></el-step>
                </el-steps>
                <el-input v-model="salary[title]" :placeholder="'请输入'+salaryItemName[index]+'...'"
                          v-for="(value,title,index) in salary"
                          :key="index" v-show="activeItemIndex===index" style="width: 200px"></el-input>
            </div>
            <span slot="footer" class="dialog-footer">
    <el-button @click="preStep">{{activeItemIndex===2?'取消':'上一步'}}</el-button>
    <el-button type="primary" @click="nextStep">{{activeItemIndex===2?'完成':'下一步'}}</el-button>
  </span>
        </el-dialog>

    </div>
</template>

<script>
    export default {
        name: "SalSob",
        data() {
            return {
                dialogVisible: false,
                dialogTitle: '添加助理套餐',
                salaries: [],
                activeItemIndex: 0,
                salaryItemName: [
                    '担任职责',
                    '午餐补助',
                    '奖金'
                ],
                salary: {
                    duty: '',
                    launch: 0,
                    reward: 0
                }
            }
        },
        mounted() {
            this.initSalaries();
        },
        methods: {
            showEditSalaryView(data) {
                this.dialogTitle = '修改助理套餐';
                this.dialogVisible = true;
                //需要手动赋值 因为顺序不一定一样
                this.salary.duty = data.duty;
                this.salary.launch = data.launch;
                this.salary.reward = data.reward;
                this.salary.id = data.id;
            },
            deleteSalary(data) {
                this.$confirm('此操作将删除第【' + data.id + '】账套，是否继续？', '提示', {
                    cancelButtonText: '取消',
                    confirmButtonText: '确定'
                }).then(() => {
                    this.deleteRequest("/system/salary/" + data.id).then(res  => {
                        if (res) {
                            this.initSalaries();
                        }
                    })
                }).catch(() => {
                    this.$message.info("取消删除!");
                })
            },
            preStep() {
                if (this.activeItemIndex === 0) {
                    return;
                } else if (this.activeItemIndex === 2) {
                    //关闭对话框
                    this.dialogVisible = false;
                    return;
                }
                this.activeItemIndex--;
            },
            nextStep() {
                if (this.activeItemIndex === 2) {
                    if (this.salary.id) {
                        this.putRequest("/system/salary/", this.salary).then(resp=>{
                            if (resp) {
                                this.initSalaries();
                                this.dialogVisible = false;
                                this.activeItemIndex = 0
                            }
                        })
                    } else {
                        this.postRequest("/system/salary/", this.salary).then(resp => {
                            if (resp) {
                                this.initSalaries();
                                this.dialogVisible = false;
                            }
                        });
                    }
                    return;
                }
                this.activeItemIndex++;
            },
            showAddSalaryView() {
                //数据初始化
                this.salary = {
                    duty: '',
                    launch: 0,
                    reward: 0
                }
                this.dialogTitle = '添加助理套餐';
                this.activeItemIndex = 0;
                this.dialogVisible = true;
            },
            initSalaries() {
                this.getRequest("/system/salary/").then(res => {
                    if (res) {
                        this.salaries = res;
                    }
                })
            }
        }
    }
</script>

<style scoped>

</style>
